package com.jbp.service;
import com.jbp.model.Gift;
import com.jbp.model.GiftLot;
import com.jbp.redis.RedisConfigService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.*;
import java.util.stream.Collectors;

/**
 飞马爆率版抽奖工具
 分大小奖池，大户走大奖池，小户走小奖池
 */
@Service
public class LuckyUtil4 {
    @Autowired
    private RedisConfigService redisConfigService;

    @Autowired
    private PropertyService propertyService;

    public List<Gift> luck(Integer userId, int times, List<GiftLot> list, BigDecimal bloodLine) {

        //    //总爆率
        double winRate = Double.parseDouble(propertyService.getV("luck_win_rate_sys"));
//    //个人最高爆率
        double personRateHigh = Double.parseDouble(propertyService.getV("luck_high_usr"));
        //奖池礼物数量
        int poolSize = Integer.parseInt(propertyService.getV("pool_size"));
        int minPoolSize = Integer.parseInt(propertyService.getV("min_pool_size"));
        int maxBlood = Integer.parseInt(propertyService.getV("max_blood"));
        double bloodRate = Double.parseDouble(propertyService.getV("blood_rate"));
        double bloodBase = Double.parseDouble(propertyService.getV("blood_base"));

        bloodBase = bloodLine==null || bloodLine.doubleValue()==0 ? bloodBase : bloodLine.doubleValue();

        //系统总产出
        int allIncome = 0;
        Integer r_allIncome = (Integer) redisConfigService.get("haoya:luck:allincome");
        if (r_allIncome!=null) {
            allIncome = r_allIncome;
        }
        //系统总投入
        int allPay = 0;
        Integer r_allPay = (Integer) redisConfigService.get("haoya:luck:allpay");
        if (r_allPay!=null) {
            allPay = r_allPay;
        }
        //当前系统爆率
        double totalRate = 1.1;
        Double r_totalRate = (Double) redisConfigService.get("haoya:luck:totalRate");
        if (r_totalRate!=null) {
            totalRate = r_totalRate;
        }

        //个人投入
        int pay = 0;
        Integer r_pay = (Integer) redisConfigService.get("haoya:luck:upay:"+userId);
        if (r_pay!=null) {
            pay = r_pay;
        }
        //个人产出
        int income = 0;
        Integer r_income = (Integer) redisConfigService.get("haoya:luck:uincome:"+userId);
        if (r_income!=null) {
            income = r_income;
        }

        //水池
        int pool = 0;
        Integer r_pool = (Integer) redisConfigService.get("haoya:luck:pool");
        if (r_pool!=null) {
            pool = r_pool;
        }

        //奖池剩余礼物数量
        int giftCount = 0;
        Integer r_giftCount = (Integer) redisConfigService.get("haoya:luck:giftcount");
        if (r_giftCount!=null) {
            giftCount = r_giftCount;
        }

        //生成奖池
        if (giftCount<=minPoolSize) {
            giftCount += poolSize;
            pool += poolSize * winRate * 20;
            System.out.println("生成奖池："+pool);
        }

        List<Gift> result = new ArrayList<>();
        for (int i = 0; i < times; i++) {
            giftCount --;
            pay += 20;
            allPay += 20;

            //剩余爆率， 根据剩余爆率控制用户爆率在1.2到personRateHigh之间
            double rate = BigDecimal.valueOf(pool).divide(BigDecimal.valueOf(giftCount*20), 4, BigDecimal.ROUND_HALF_UP).doubleValue();

            personRateHigh = Math.min(Math.max(3*rate*rate, 1.18), personRateHigh);

            //用户最大奖限制
            int biggest = (int) Math.min(pool/3, personRateHigh * pay - income);

            if (rate<0.48) {
                biggest = Math.min(biggest, 520);
            } else if (rate<0.68) {
                biggest = Math.min(biggest, 999);
            } else if (rate<0.86) {
                biggest = Math.min(biggest, 2333);
            } else if (rate<1.02) {
                biggest = Math.min(biggest, 5200);
            } else if (rate<1.08) {
                biggest = Math.min(biggest, 10001);
            } else if (rate<1.11) {
                biggest = Math.min(biggest, 18888);
            } else if (rate<1.13) {
                biggest = Math.min(biggest, 33440);
            }

            biggest = Math.min(biggest, 52000);
            biggest = Math.max(biggest, 52);

            //血
            int blood = Math.min(maxBlood, Math.max((int) ((bloodBase * pay - income)*bloodRate), 0));

            //抽奖
            Gift gift = luck(biggest, list, blood);
            income += gift.getCoin();
            allIncome += gift.getCoin();
            pool -= gift.getCoin();

            result.add(gift);
        }

        redisConfigService.set("haoya:luck:allincome", allIncome);
        redisConfigService.set("haoya:luck:allpay", allPay);
        redisConfigService.set("haoya:luck:totalRate", totalRate);
        redisConfigService.set("haoya:luck:pool", pool);
        redisConfigService.set("haoya:luck:giftcount", giftCount);
        redisConfigService.set("haoya:luck:upay:"+userId, pay);
        redisConfigService.set("haoya:luck:uincome:"+userId, income);
        return result;
    }

    private Gift luck(double biggest, List<GiftLot> giftLots, int blood) {
        List<GiftLot> pool = giftLots.stream().filter(giftLot -> giftLot.getGift().getCoin() <= biggest)
                .collect(Collectors.toList());

        int sum = pool.stream().mapToInt(giftLot->giftLot.getGiftNum()).sum();
        int i = new Random().nextInt(sum-blood)+blood;
        int t = 0;
        for (GiftLot giftLot : pool) {
            t += giftLot.getGiftNum();
            if (i<=t) {
                return giftLot.getGift();
            }
        }
        throw new RuntimeException("抽奖错误");
    }

    public List<Gift> luckTora(Integer userId, int times, List<GiftLot> list) {
        //    //总爆率
        double winRate = 1.17;
//    //个人最高爆率
        double personRateHigh = 1.50;

        //个人投入
        int pay = 0;
        Integer r_pay = (Integer) redisConfigService.get("haoya:luck:upay:"+userId);
        if (r_pay!=null) {
            pay = r_pay;
        }

        //个人产出
        int income = 0;
        Integer r_income = (Integer) redisConfigService.get("haoya:luck:uincome:"+userId);
        if (r_income!=null) {
            income = r_income;
        }

        List<Gift> result = new ArrayList<>();
        for (int i = 0; i < times; i++) {
            pay += 20;
            //用户最大奖限制
            int biggest = (int) Math.min(winRate * pay - income, personRateHigh * pay - income);
            biggest = Math.max(biggest, 52);

            //血
            int blood = Math.min(100000, Math.max((int) (winRate * pay - income)*2, 0));

            //抽奖
            Gift gift = luck(biggest, list, blood);
            income += gift.getCoin();
            result.add(gift);
        }
        redisConfigService.set("haoya:luck:upay:"+userId, pay);
        redisConfigService.set("haoya:luck:uincome:"+userId, income);
        return result;
    }

    //打印统计
    private static void printTj(ArrayList<Double> bls , int times){
        System.out.println("总计抽奖:"+bls.size()+"回， 每回"+times+"次");
        System.out.println("爆率<30:"+bls.stream().filter(bl->bl<30).count()+"次");
        System.out.println("30<=爆率<40:"+bls.stream().filter(bl->bl>=30 && bl<40).count()+"次");
        System.out.println("40<=爆率<50:"+bls.stream().filter(bl->bl>=40 && bl<50).count()+"次");
        System.out.println("50<=爆率<60:"+bls.stream().filter(bl->bl>=50 && bl<60).count()+"次");
        System.out.println("60<=爆率<70:"+bls.stream().filter(bl->bl>=60 && bl<70).count()+"次");
        System.out.println("70<=爆率<80:"+bls.stream().filter(bl->bl>=70 && bl<80).count()+"次");
        System.out.println("80<=爆率<90:"+bls.stream().filter(bl->bl>=80 && bl<90).count()+"次");
        System.out.println("90<=爆率<100:"+bls.stream().filter(bl->bl>=90 && bl<100).count()+"次");
        System.out.println("100<=爆率<110:"+bls.stream().filter(bl->bl>=100 && bl<110).count()+"次");
        System.out.println("110<=爆率<120:"+bls.stream().filter(bl->bl>=110 && bl<120).count()+"次");
        System.out.println("120<=爆率<130:"+bls.stream().filter(bl->bl>=120 && bl<130).count()+"次");
        System.out.println("130<=爆率<140:"+bls.stream().filter(bl->bl>=130 && bl<140).count()+"次");
        System.out.println("140<=爆率<150:"+bls.stream().filter(bl->bl>=140 && bl<150).count()+"次");
        System.out.println("爆率>=150:"+bls.stream().filter(bl->bl>=150).count()+"次");
    }
    //打印爆率
    private static void printBl(ArrayDeque<Integer> gifts, int times){
        while (gifts.size()>=times) {
            List<Integer> lucks = new ArrayList<>();
            for (int i = 0; i < times; i++) {
                lucks.add(gifts.pop());
            }
            int cc = lucks.stream().mapToInt((i) -> i).sum();
            BigDecimal bl = BigDecimal.valueOf(cc * 100).divide(BigDecimal.valueOf(times * 20), 4, BigDecimal.ROUND_HALF_UP);

            System.out.println("抽奖"+times+"次 投入:"+times*20+" 产出："+cc + "  爆率:"+bl);
        }
    }

    private static void printConfig(Map<Integer, Integer> map) {
        //总价值
        long totalCoin = map.entrySet().stream().mapToInt(e -> e.getKey()*e.getValue()).sum();
        //总数量
        long totalSize = map.values().stream().mapToInt(v->v).sum();
        double zbl = BigDecimal.valueOf(totalCoin).divide(BigDecimal.valueOf(totalSize*20), 6, BigDecimal.ROUND_HALF_UP).doubleValue();

        for (Map.Entry<Integer, Integer> e : map.entrySet()) {
            double bl = BigDecimal.valueOf(e.getValue() * 100).divide(BigDecimal.valueOf(totalSize), 6, BigDecimal.ROUND_HALF_UP).doubleValue();
            double zb = BigDecimal.valueOf(e.getValue() * e.getKey() * 100).divide(BigDecimal.valueOf(totalCoin), 6, BigDecimal.ROUND_HALF_UP).doubleValue();

            System.out.println(e.getKey()+"钻  "+ e.getValue()+"个   "+" 爆率:"+ bl +"    占比:"+zb);
        }
        System.out.println("总数量："+totalSize+"  总价值:"+totalCoin+"  总爆率："+zbl);
    }


    private int luckt(double biggest, Map <Integer, Integer> sorce, int blood) {
        Map <Integer, Integer> pool = new HashMap<>();
        sorce.keySet().stream().filter(i -> i<=biggest).forEach(k->{
            pool.put(k, sorce.get(k));
        });
        int sum = pool.values().stream().mapToInt(Integer::intValue).sum();
//        System.out.println("sum:"+sum+" blood:"+blood+"  big:"+biggest);
        int i = new Random().nextInt(sum-blood)+blood;
        int t = 0;
        for (Map.Entry<Integer, Integer> entry : pool.entrySet()) {
            t += entry.getValue();
            if (i<=t) {
                return entry.getKey();
            }
        }
        throw new RuntimeException("抽奖错误");
    }
    Pool poolA = new Pool();
    Pool poolB = new Pool();
    Pool poolC = new Pool();
    @SuppressWarnings("AliControlFlowStatementWithoutBraces")
    BigDecimal luckTest(UserLuck userLuck, Map <Integer, Integer> out) {
        if (poolA.map==null) poolA.map=getGiftCoinfigA();
        if (poolB.map==null) poolB.map=getGiftCoinfigB();
        if (poolC.map==null) poolC.map=getGiftCoinfigC();
        //总爆率
        double winRate = 1.15;
        //个人最高爆率
        double personRateHigh = 10;
//        //个人投入
//        int pay = 0;
//        //个人产出
//        int income = 0;

        Pool pl = poolA;
        if (userLuck.pay<10000) {
            pl = poolA;
        } else if (userLuck.pay<100000) {
            pl = poolB;
        } else {
            pl = poolC;
        }

//        Map <Integer, Integer> map = pl.map;
//        //系统总产出
//        int allIncome = pl.allIncome;
//        //系统总投入
//        int allPay = pl.allPay;
//        //水池
//        int pool = pl.pool;
//        //奖池剩余礼物数量
//        int giftCount = 0;
//        int giftSize = 50000;


        ArrayList<Integer> lucks = new ArrayList<>();

        if (pl.pool>0 && userLuck.pay==0) {
            double rate1 = BigDecimal.valueOf(pl.pool).divide(BigDecimal.valueOf(pl.giftCount*20), 4, BigDecimal.ROUND_HALF_UP).doubleValue();
//            System.out.println("剩余礼物:"+pl.giftCount+"    水池:"+pl.pool +"   爆率："+rate1);
//            try {
//                Thread.sleep(10);
//            } catch (InterruptedException e) {
//                e.printStackTrace();
//            }
        }
        for (int i = 0; i < 1; i++) {
            //生成奖池
            if (pl.giftCount<=2000) {
                pl.giftCount += pl.giftSize;
                pl.pool += pl.giftSize * winRate * 20;
//                System.out.println("生成奖池："+ pl.pool);
            }

            pl.giftCount --;
            userLuck.pay += 20;
            pl.allPay +=20;


            //剩余爆率
            double rate = BigDecimal.valueOf(pl.pool).divide(BigDecimal.valueOf(pl.giftCount*20), 4, BigDecimal.ROUND_HALF_UP).doubleValue();

//            personRateHigh = Math.min(Math.max(3*rate*rate, 1.18), personRateHigh);

            //用户最大奖限制
            int biggest = (int) Math.min(pl.pool/3, personRateHigh * userLuck.pay - userLuck.income);


            int c = 0, t=0;
            for (Map.Entry<Integer, Integer> entry : pl.map.entrySet()) {
                c += entry.getValue();
                t += entry.getKey()*entry.getValue();
                double r = t/20d/c;
                if (rate<r) {
                    biggest = Math.min(biggest, entry.getKey());
//                    System.out.println(rate+"---"+biggest);
                    break;
                }
            }

            biggest = Math.min(biggest, 52000);
            biggest = Math.max(biggest, 52);

            //血
            int blood = Math.min(100000, Math.max((int) ((userLuck.bloodBase * userLuck.pay - userLuck.income)*5), 0));
//            blood = 0;

            //抽奖
            int luck = luckt(biggest, pl.map, blood);
            lucks.add(luck);
            userLuck.income += luck;
            pl.allIncome += luck;
            pl.pool -= luck;

            if (out.containsKey(luck)) {
                out.put(luck, out.get(luck)+1);
            } else {
                out.put(luck, 1);
            }
        }

        BigDecimal rate = BigDecimal.valueOf(userLuck.income).divide(BigDecimal.valueOf(userLuck.pay), 2, BigDecimal.ROUND_HALF_UP);
        BigDecimal allRate = BigDecimal.valueOf(pl.allIncome).divide(BigDecimal.valueOf(pl.allPay), 4, BigDecimal.ROUND_HALF_UP);
//        System.out.println("抽奖：" +1+"次，爆率:"+rate +" 总爆率："+allRate + "  水池："+pl.pool);
        return rate;
    }

    private static Map <Integer, Integer> getGiftCoinfig(){
        Map <Integer, Integer> map = new LinkedHashMap<>();
        map.put(1, 116833);
        map.put(5, 19999);
        map.put(20, 5001);
        map.put(52, 3691);
        map.put(100, 1550);
        map.put(200, 780);
        map.put(520, 1200);
        map.put(999, 600);
        map.put(2333, 235);
        map.put(5200, 85);
        map.put(10001, 19);
        map.put(18888, 3);
        map.put(33440, 2);
        map.put(52000, 1);
        return map;
    }

    /**
     * 小号奖池 投入1w钻以内
     */
    private static Map <Integer, Integer> getGiftCoinfigA() {
        Map<Integer, Integer> map = new LinkedHashMap<>();
        /*map.put(1, 91402);
        map.put(10, 34740);
        map.put(20, 7005);
        map.put(52, 7000);
        map.put(99, 5500);
        map.put(199, 2010);
        map.put(520, 1730);
        map.put(1000, 577);
        map.put(1888, 32);
        map.put(5200, 3);
        map.put(11111, 1);*/
        map.put(1, 124589);
        map.put(10, 15705);
        map.put(20, 2000);
        map.put(52, 1500);
        map.put(99, 800);
        map.put(199, 440);
        map.put(520, 3840);
        map.put(1000, 820);
        map.put(1888, 25);
        map.put(5200, 5);
        return map;
    }
    /**
     * 中号奖池 投入10w钻以内
     */
    private static Map <Integer, Integer> getGiftCoinfigB() {
        Map<Integer, Integer> map = new LinkedHashMap<>();
        /*map.put(1, 92174);
        map.put(10, 34740);
        map.put(20, 7005);
        map.put(52, 7300);
        map.put(99, 4620);
        map.put(199, 1810);
        map.put(520, 1730);
        map.put(1000, 577);
        map.put(1888, 32);
        map.put(5200, 6);
        map.put(11111, 3);
        map.put(20200, 2);
        map.put(33440, 1);*/
        /*map.put(1, 125596);
        map.put(10, 15720);
        map.put(20, 2000);
        map.put(52, 1500);
        map.put(99, 800);
        map.put(199, 440);
        map.put(520, 2733);
        map.put(1000, 1125);
        map.put(1888, 60);
        map.put(5200, 18);
        map.put(11111, 5);
        map.put(20200, 2);
        map.put(33440, 1);*/
        map.put(1, 127011);
        map.put(10, 15720);
        map.put(20, 1500);
        map.put(52, 1000);
        map.put(99, 800);
        map.put(199, 400);
        map.put(520, 2233);
        map.put(1000, 1125);
        map.put(1888, 170);
        map.put(5200, 30);
        map.put(11111, 8);
        map.put(20200, 2);
        map.put(33440, 1);
        return map;
    }

    /**
     * 大号奖池 投入20w钻以上
     */
    private static Map <Integer, Integer> getGiftCoinfigC() {
        Map<Integer, Integer> map = new LinkedHashMap<>();
        /*map.put(1, 98770);
        map.put(10, 29740);
        map.put(20, 7005);
        map.put(52, 7323);
        map.put(99, 3820);
        map.put(199, 1414);
        map.put(520, 1230);
        map.put(1000, 480);
        map.put(1888, 175);
        map.put(5200, 30);
        map.put(11111, 6);
        map.put(20200, 4);
        map.put(33440, 3);
        map.put(52000, 2);*/
        map.put(1, 134312);
        map.put(10, 11720);
        map.put(20, 1000);
        map.put(52, 500);
        map.put(99, 300);
        map.put(199, 200);
        map.put(520, 1185);
        map.put(1000, 480);
        map.put(1888, 370);
        map.put(5200, 85);
        map.put(11111, 28);
        map.put(20200, 12);
        map.put(33440, 6);
        map.put(52000, 2);
        return map;
    }

    private static void tjUser(List<UserLuck> userLucks) {
        //总人数
        int totalCount = userLucks.size();
        //盈利人数
        int winCount = (int) userLucks.stream().filter(user->user.income/user.pay>1.17d && user.income-user.pay*1.17d>200).count();
        //总盈利
        int sumWin = userLucks.stream().filter(user -> user.income / user.pay > 1.17d && user.income - user.pay * 1.17d > 200).mapToInt(user -> (int) (user.income - user.pay * 1.17d)).sum();
        // 平均盈利
        int avgWin = sumWin/winCount;
        //最大盈利
        int maxWin = userLucks.stream().filter(user -> user.income / user.pay > 1.17d && user.income - user.pay * 1.17d > 200).mapToInt(user -> (int) (user.income - user.pay * 1.17d)).max().getAsInt();

        //亏损人数
        int loseCount = totalCount - winCount;
        //总亏损
        int sumLose = userLucks.stream().filter(user -> (user.income / user.pay <= 1.17d) || (user.income - user.pay * 1.17d < 200)).mapToInt(user -> (int) (user.income - user.pay * 1.17d)).sum();
        //平均亏损
        int avgLose = sumLose/loseCount;
        //最大亏损
        int maxLose = userLucks.stream().filter(user -> (user.income / user.pay <= 1.17d) || (user.income - user.pay * 1.17d < 200)).mapToInt(user -> (int) (user.income - user.pay * 1.17d)).min().getAsInt();

        System.out.println("抽奖总人数："+totalCount);
        System.out.println("盈利人数:"+winCount+"  平均盈利:"+avgWin+" 最大盈利："+maxWin);
        System.out.println("亏损人数:"+loseCount+"  平均亏损:"+avgLose+" 最大亏损："+maxLose);

        System.out.println("---用户亏损统计----");
        System.out.println("亏损大于10w钻： "+userLucks.stream().filter(user -> user.income - user.pay * 1.17d < -100000).count());
        System.out.println("亏损5w-10w钻： "+userLucks.stream().filter(user -> user.income - user.pay * 1.17d >= -100000 && user.income - user.pay * 1.17d < -50000).count());
        System.out.println("亏损<5w钻： "+userLucks.stream().filter(user -> user.income - user.pay * 1.17d >= -50000&& user.income - user.pay * 1.17d < 0).count());
        System.out.println("---用户盈利统计----");
        System.out.println("200<盈利<1000钻: "+userLucks.stream().filter(user -> user.income - user.pay * 1.17d >= 200 && user.income - user.pay * 1.17d < 1000).count());
        System.out.println("1000<盈利<2000钻: "+userLucks.stream().filter(user -> user.income - user.pay * 1.17d >= 1000 && user.income - user.pay * 1.17d < 2000).count());
        System.out.println("2000<盈利<5000钻： "+userLucks.stream().filter(user -> user.income - user.pay * 1.17d >= 2000 && user.income - user.pay * 1.17d < 5000).count());
        System.out.println("5000<盈利<10000钻： "+userLucks.stream().filter(user -> user.income - user.pay * 1.17d >= 5000 && user.income - user.pay * 1.17d < 10000).count());
        System.out.println("10000<盈利<100000钻： "+userLucks.stream().filter(user -> user.income - user.pay * 1.17d >= 10000 && user.income - user.pay * 1.17d < 100000).count());

        System.out.println("---用户爆率统计----");
        System.out.println("爆率<0.9： "+userLucks.stream().filter(user -> user.income*1d / user.pay < 0.9d).count());
        System.out.println("0.9<爆率<1： "+userLucks.stream().filter(user -> user.income*1d / user.pay < 1d && user.income*1d / user.pay >= 0.9d).count());
        System.out.println("1<爆率<1.1："+userLucks.stream().filter(user -> user.income*1d / user.pay < 1.1d && user.income*1d / user.pay >= 1d).count());
        System.out.println("1.1<爆率<1.15："+userLucks.stream().filter(user -> user.income*1d / user.pay < 1.15d && user.income*1d / user.pay >= 1.1d).count());
        System.out.println("1.15<爆率<1.2："+userLucks.stream().filter(user -> user.income*1d / user.pay < 1.2d && user.income*1d / user.pay >= 1.15d).count());
        System.out.println("1.2<爆率<1.3："+userLucks.stream().filter(user -> user.income*1d / user.pay < 1.3d && user.income*1d / user.pay >= 1.2d).count());
        System.out.println("1.3<爆率<1.5："+userLucks.stream().filter(user -> user.income*1d / user.pay < 1.5d && user.income*1d / user.pay >= 1.3d).count());
        System.out.println("1.5<爆率<1.8："+userLucks.stream().filter(user -> user.income*1d / user.pay < 1.8d && user.income*1d / user.pay >= 1.5d).count());
        System.out.println("1.8<爆率<2.5："+userLucks.stream().filter(user -> user.income*1d / user.pay < 2.5d && user.income*1d / user.pay >= 1.8d).count());
        System.out.println("2.5<爆率<5："+userLucks.stream().filter(user -> user.income*1d / user.pay < 5d && user.income*1d / user.pay >= 2.5d).count());
        System.out.println("5<爆率："+userLucks.stream().filter(user -> user.income*1d / user.pay >= 5d).count());
    }

    public static void main(String[] args) {
        System.out.println("----------小奖池投入1w钻以下----------");
        printConfig(getGiftCoinfigA());
        System.out.println("----------中奖池投入10w钻以下----------");
        printConfig(getGiftCoinfigB());
        System.out.println("---------------大奖池-----------------");
        printConfig(getGiftCoinfigC());
        System.out.println("--------------模拟规则----------------\n" +
                "1. 按117爆率计算用户盈亏\n" +
                "2. 盈利大于500钻停止抽奖\n" +
                "3. 亏损大于20w钻停止抽奖\n" +
                "4. 总投入大于200w钻停止抽奖");
        System.out.println("--------------模拟抽奖-----------------");
        List<UserLuck> userLucks = new ArrayList<>();
        Map <Integer, Integer> result = new LinkedHashMap<>();
        LuckyUtil4 luckyUtil = new LuckyUtil4();
        ArrayList<Double> list = new ArrayList<>();
        int times = 10000;
        for (int i = 0; i < times; i++) {
            UserLuck user = new UserLuck();
            user.bloodBase = i%10==0 ? 1.15 : 1.05;
            while (true) {
                luckyUtil.luckTest(user, result);
                if (user.pay>10000 || (user.income-user.pay*1.17d>500) || (user.income-user.pay*1.17d<-200000)) {
                    break;
                }
            }
            userLucks.add(user);
            double rate  = BigDecimal.valueOf(user.income).divide(BigDecimal.valueOf(user.pay), 4, BigDecimal.ROUND_HALF_UP).doubleValue();
//            System.out.println(i+": 投入："+user.pay+" 产出:"+user.income +"  爆率:"+rate+ " 实际盈利："+(int)(user.income-user.pay*1.17));
        }
//        printTj(list, times);
        tjUser(userLucks);

        System.out.println("------------系统同爆率------------");
        System.out.println("A 系统投入:"+luckyUtil.poolA.allPay+" 产出:"+luckyUtil.poolA.allIncome+" 总爆率:"+ ((double)luckyUtil.poolA.allIncome/luckyUtil.poolA.allPay));
        System.out.println("B 系统投入:"+luckyUtil.poolB.allPay+" 产出:"+luckyUtil.poolB.allIncome+" 总爆率:"+ ((double)luckyUtil.poolB.allIncome/luckyUtil.poolB.allPay));
        System.out.println("C 系统投入:"+luckyUtil.poolC.allPay+" 产出:"+luckyUtil.poolC.allIncome+" 总爆率:"+ ((double)luckyUtil.poolC.allIncome/luckyUtil.poolC.allPay));
        System.out.println("总 系统投入:"+(luckyUtil.poolA.allPay+luckyUtil.poolB.allPay+luckyUtil.poolC.allPay)+
                " 产出:"+(luckyUtil.poolA.allIncome+luckyUtil.poolB.allIncome+luckyUtil.poolC.allIncome)+
                " 总爆率:"+ ((double)(luckyUtil.poolA.allIncome+luckyUtil.poolB.allIncome+luckyUtil.poolC.allIncome)/(luckyUtil.poolA.allPay+luckyUtil.poolB.allPay+luckyUtil.poolC.allPay)));


        System.out.println("------------礼物次数统计-----------");
        result.keySet().stream().sorted().forEach(k->{
            System.out.println("抽到:"+k+"   次:"+result.get(k));
        });
    }
}

class Pool{
    //系统总产出
    long allIncome = 0;
    //系统总投入
    long allPay = 0;
    //水池
    int pool = 0;
    //奖池剩余礼物数量
    int giftCount = 0;
    int giftSize = 20000;
    Map <Integer, Integer> map = null;
    double bloodRate=0;
}
class UserLuck2{
    double pay;
    double income;
    double bloodBase;
}